package com.yufa.springcloud.controller;

import com.yufa.springcloud.config.MyStandardMultipartFile;
import com.yufa.springcloud.feign.FeignClient01;
import org.apache.catalina.core.ApplicationPart;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileItemFactory;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.util.FileItemHeadersImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Part;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

@RestController
public class FileConstroller {

    @Autowired
    private FeignClient01 feignClient01;

    @PostMapping(value = "/upload")
    private List<String> upload(@RequestPart(value = "file", required = false) MultipartFile mFile) throws IOException {
        String path = "/C:\\Users\\huangyufa\\Desktop\\1.txt";
        if (mFile == null) {
            File file = new File(path);
//        FileInputStream inputStream = null;
//        inputStream = new FileInputStream(file);
//        MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(),
//                "application/octet-stream", inputStream);


//        FileItemFactory factory = new DiskFileItemFactory(16, file);
//        FileItem item = factory.createItem(file.getName(), "application/octet-stream", true, file.getName());
//        MultipartFile multipartFile = new CommonsMultipartFile(item);
            FileItem fileItem = createFileItem(path,file);
            Part part = new ApplicationPart(fileItem, file);
            mFile = new MyStandardMultipartFile(part, "1.txt");

        }
        List<String> upload = feignClient01.upload(mFile);
        return upload;
    }

    //1.首先获取FileItem对象：
    public FileItem createFileItem(String filePath,File file22 ) {
        FileItemFactory factory = new DiskFileItemFactory(0, null);
        String textFieldName = "textField";
        int num = filePath.lastIndexOf(".");
        String extFile = filePath.substring(num);
        FileItem item = factory.createItem(textFieldName, "text/plain", true, "MyFileName");

        File newfile = new File(filePath);
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        try {
            FileInputStream fis = new FileInputStream(newfile);
            OutputStream os = item.getOutputStream();
            while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileItemHeadersImpl headers=new FileItemHeadersImpl();
         headers.addHeader("content-disposition","form-data; name=\"file\"; filename=\"1.txt\"");
         headers.addHeader("content-type","text/plain");
        item.setHeaders(headers);
        item.setFormField(false);
        return item;
    }
}
